package com.lixh.virtual.ui.main;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.OrientationHelper;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lixh.base.BaseActivity;
import com.lixh.base.QuickPage;
import com.lixh.bean.Message;
import com.lixh.rxhttp.Observable;
import com.lixh.utils.ULog;
import com.lixh.view.LoadView;
import com.lixh.view.UToolBar;
import com.lixh.virtual.IExtra;
import com.lixh.virtual.R;
import com.lixh.virtual.app.os.bean.AppData;
import com.lixh.virtual.app.os.bean.AppInfoLite;
import com.lixh.virtual.ui.appList.ListAppActivity;
import com.lixh.virtual.ui.main.adapter.MainLaunchAdapter;
import com.lixh.virtual.ui.main.presenter.MainPresenter;

import java.util.List;


/**
 * Created by LIXH on 2016/12/21.
 * email lixhVip9@163.com
 * des
 */
public class MainActivity extends BaseActivity<MainPresenter> {

    QuickPage quickPage;
    MainLaunchAdapter adapter;

    @Override
    public boolean isShowBack() {
        return false;
    }

    @Override
    public boolean isDoubleExit() {
        return true;
    }

    @Override
    public void initLoad(LoadView.Builder builder) {
        builder.swipeBack = false;
        super.initLoad(builder);
    }

    @Override
    public int getLayoutId() {
        return 0;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        GridLayoutManager layoutManager = new GridLayoutManager(this, 3);
        quickPage = QuickPage.with(this)
                .setLayoutManager(layoutManager)
                .setAdapter(adapter = new MainLaunchAdapter())
                .setOnLoadingListener((page) -> mPresenter.getVirtualApps())
                .build();
        layout.setContentView(quickPage.getRootView());
        adapter.setOnItemClickListener((adapter, view, position) -> {
            List<AppData> appDatas = adapter.getData();
            AppData appData = appDatas.get(position);
            if (!appData.isLoading()) {
                mPresenter.launchApp(appData);
            }
        });
        adapter.setOnItemLongClickListener((adapter, view, position) -> {
            List<AppData> appDatas = adapter.getData();
            AppData appData = appDatas.get(position);
            if (!appData.isLoading()) {
                mPresenter.deleteApp(appData);
            }
            return false;
        });

    }

    @Override
    public void initTitle(UToolBar toolBar) {
        //添加分身
        toolBar.setRightImage(R.drawable.ic_add_black, v -> intent.goForResult(ListAppActivity.class, IExtra.REQUEST_SELECT_APP));
    }

    public void addAppToLauncher(AppData appData) {
        adapter.addData(appData);
        quickPage.smoothScrollToPosition(adapter.getItemCount() - 1);

    }


    public void removeAppToLauncher(AppData appData) {
        if (adapter.getData().remove(appData)) {
            adapter.notifyDataSetChanged();
        }
    }

    public void refreshLauncherItem(AppData model) {
        int index = adapter.getData().indexOf(model);
        if (index >= 0) {
            adapter.notifyItemChanged(index);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && data != null) {
            List<AppInfoLite> appList = data.getParcelableArrayListExtra(IExtra.KEY_APP_INFO_LIST);
            if (appList != null) {
                for (AppInfoLite info : appList) {
                    mPresenter.addApp(info);
                }
            }
        }
    }

    @Override
    public void update(Observable o, Message arg) {
        ULog.e(arg.toString());
    }


    public void setAppData(List<AppData> appData) {
        quickPage.addData(appData);
    }
}
